package com.fz.song.web.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.fz.song.framework.common.R;
import com.fz.song.web.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "幂等性demo-mq消费")
@Slf4j
@RestController
@RequestMapping("idempotent")
public class MqMessageController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @ApiOperation(value = "推送mq消息", notes = "推送mq消息")
    @GetMapping("/push")
    public R push() {
        User user = new User();
        user.setName("push");
        user.setIdCard(IdUtil.simpleUUID());
        user.setVersion(1);
        user.setMoney(1000);
        user.setStock(1000);
        rocketMQTemplate.syncSend("test-topic:user", MessageBuilder.withPayload(JSONUtil.toJsonStr(user)).build());
        return R.ok();
    }

}
